package com.runa.rccprotocol.analysis.combination;

import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.EnAnalysis;
import com.runa.rccprotocol.analysis.lichuang.LiChuangEquDataAnalysis;
import com.runa.rccprotocol.analysis.zhongrun.ZhongRunMeterDataAnalysis;

import java.util.HashMap;
import java.util.Map;

/**
 * 当一个纯透传集中器（不带集中器的包头和包尾）下挂接了，中润厂家的设备，力创厂家的设备，欧标的设备，就可以用此协议解析
 * TC_Combination  数据协议
 * @author diandian
 * @Date 2022/4/28 10:22
 */
public class TouChuanCombinationAnalysis extends MetaBufferAnalysis<byte[]> {

    private Map<Integer, MetaBufferAnalysis<byte[]>> analysisMap = new HashMap<>();

    ZhongRunMeterDataAnalysis zhongRunMeterDataAnalysis = new ZhongRunMeterDataAnalysis();
    LiChuangEquDataAnalysis liChuangEquDataAnalysis = new LiChuangEquDataAnalysis();
    EnAnalysis enAnalysis = new EnAnalysis();

    public TouChuanCombinationAnalysis() {
    }


    @Override
    public int fill(byte[] data, int length,String netEquNo) {
        //解析表阀数据，这里必须要挨个匹配才能确定用什么协议解析才最合适
        int type = getAnalysis(data, length);
        //1，中润      2，力创        3，欧标
        //-1 表示需要续长度
        //0 表示丢弃，匹配不上
        MetaBufferAnalysis<byte[]> analysis = null;

        switch (type){
            case -1:
                return -1;
            case 0:
                return 0;
            case 1:
                analysis = zhongRunMeterDataAnalysis;
                break;
            case 2:
                analysis = liChuangEquDataAnalysis;
                break;
            case 3:
                analysis = enAnalysis;
                break;
        }
        //到这里analysis 肯定是有值的了
        int fill = analysis.fill(data, length,netEquNo);
        this.responseData = (IResponseData) analysis.gain();
        return fill;
    }

    private int getAnalysis(byte[] data, int length){
        //1，中润      2，力创        3，欧标
        // -1 表示需要续长度
        //0 表示丢弃，匹配不上
        short type = 0;
        //验证欧标
        if (0xE5 == (data[0] & 0xff)) {
            type = 3;
            return type;
        }
        //验证是否是欧标协议，通过68 xx xx 68 判断
        for (int i = 0; i < data.length; i++) {
            boolean b = (data[i] & 0xff) == 0x68;
            if (b) {
                //先校验长度
                if (length >= (i + 1) + 3) {
                    boolean lenMatch = data[i + 1] == data[i + 2];
                    boolean startMatch = data[i] == data[i + 3];
                    if (lenMatch && startMatch) {
                        type = 3;
                        return type;
                    }
                } else {
                    return -1;
                }
            }
        }

        //中润或者是力创的设备
        int begin = -1;
        for (int i = 0; i < length; i++) {
            if (0x68 == (data[i] & 0xff)) {
                begin = i;
                break;
            }
        }

        if (begin == -1) {
            return 0;
        }

        //11字节到数据长度，2个字节位结尾的校验、结束字节
        if (length < begin + 11 + 2) {
            return -1;
        }

        int index = begin;
        //表设备
        ++index;
        boolean meterEqu = (data[index] & 0xff) == 0x20;
        //设备编号
        ++index;
        String equSerialNo = ByteUtil.byteToHexStringFlip(data, index, 4);
        index += 4;
        //厂商代码  其中 A5(59),A6(42)为厂商代码
        String factoryCode = ByteUtil.byteToHexString(data, index, 3);
        //中润厂家 001111
        if ("001111".equals(factoryCode)) {
            return 1;
        }
        //力创厂家 005942(表)  005146(阀门)
        if ("005942".equals(factoryCode) || "005146".equals(factoryCode)) {
            return 2;
        }
        return 0;
    }



    public IInterData gain() {
        return this.responseData;
    }


    public static void main(String[] args) {
        String lichuang = "FE FE 68 50 95 13 86 36 00 51 46 81 2E 90 1F 00 01 00 00 00 00 00 00 00 00 00 00 10 00 35 00 00 05 00 FF FF 37 00 00 00 00 00 20 00 80 10 00 00 00 55 55 55 55 55 55 55 64 80 00 78 16"
                .replace(" ","");
        byte[] bytes = ByteUtil.hexStringToByte(lichuang);
        TouChuanCombinationAnalysis analysis = new TouChuanCombinationAnalysis();
        analysis.fill(bytes, bytes.length,"");
    }

}